package foolcat.tools;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Spinner;

public class IBTWordsActivity extends Activity {
	public WordList wl = new WordList();
	public String[] books;
	public int listCount;
	
	public Spinner spin_book;
	public Spinner spin_speed;
	public Spinner spin_list;
	public RadioButton rad_local;
	public RadioButton rad_tts;
	
	public static final String BOOKNAME = "BOOKNAME";
	public static final String LISTNUM = "LISTNUM";
	public static final String SPEED = "SPEED";
	public static final String SNDSOURCE = "SNDSOURCE";
	
	
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

    	super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        

        spin_book = (Spinner) findViewById(R.id.spinner1);
        spin_list = (Spinner) findViewById(R.id.spinner2);
        spin_speed = (Spinner) findViewById(R.id.spinner3);
        rad_local = (RadioButton) findViewById(R.id.radio0);
        rad_tts = (RadioButton) findViewById(R.id.radio1);
        
        books = wl.getBooksFromSD();
        
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, books);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spin_book.setAdapter(adapter1);
       
        updateSpinnerList2(books[0]);
        spin_book.setOnItemSelectedListener(new OnItemSelectedListener(){
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { 
                Log.d(this.getClass().getName(), "pos=:"+pos);
            	updateSpinnerList2(books[pos]);
            }
            public void onNothingSelected(AdapterView<?> parent) {
                // Do nothing.
            }
        });
        
        ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(
                this, R.array.speed_list, android.R.layout.simple_spinner_item);
        adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spin_speed.setAdapter(adapter3);
        spin_speed.setSelection(3);
        
        Button btn_start = (Button)findViewById(R.id.button1);
        btn_start.setOnClickListener( new OnClickListener(){
        	public void onClick(View v) {
        		Intent intent = new Intent();
        	    intent.setClass(IBTWordsActivity.this, IBTWordsActivity2.class);
        	    Bundle bundle = new Bundle();
        	    bundle.putString(BOOKNAME, (String)spin_book.getSelectedItem());
        	    bundle.putInt(LISTNUM, spin_list.getSelectedItemPosition() + 1);
        	    bundle.putInt(SPEED, Integer.parseInt((String)spin_speed.getSelectedItem()));
        	    bundle.putInt(SNDSOURCE, 0);
        	    intent.putExtras(bundle);
        	    startActivity(intent);
        	}
        });
    }
    
    
	public String[] createBookListName(int cnt) {
    	String tmp = "";
    	for (int i = 1; i <= cnt; i++) {
			tmp += "#list" + i;
    	}
    	return tmp.substring(1).split("#");
    }

    
    private void updateSpinnerList2(String bookname) {
    	listCount = wl.getBookListCount(bookname);
        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, createBookListName(listCount));
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spin_list.setAdapter(adapter2);
    }
    
   
   
}